package com.lstimeline.utility;

/**
 * The class validates all kinds of user inputs for register and login functionalities.
 * @author Harry
 *
 */
public class Validation {
	private static final int USERNAME_LOWER_CONSTRAIT = 3;
	private static final int USERNAME_UPPER_CONSTRAIT = 20;
	private static final int PASSWORD_LOWER_CONSTRAIT = 6;
	private static final int PASSWORD_UPPER_CONSTRAIT = 20;
	private static final int EMAIL_UPPER_CONSTRAIT = 50;
	private static final int ACES2_UPPER_CONSTRAIT = 30;
	private static final int GENERAL_UPPER_CONSTRAIT = 50;
	
	/**
	 * To validate username input
	 * case insensitive
	 * No space or special character allowed.
	 * @param input
	 * 		username input
	 * @return whether the username is well formed
	 */
	public static boolean isUsernameWellFormed(String input) {
		if(input == null || input.length() < USERNAME_LOWER_CONSTRAIT ||
		   input.length() > USERNAME_UPPER_CONSTRAIT) {
			return false;
		}
		return input.matches("[a-zA-Z0-9_]+");
	}
	
	/**
	 * To validate password input
	 * Case sensitive, must contain at least one special character
	 * @param input
	 * 		password input
	 * @return whether the password is well formed
	 */
	public static boolean isPasswordWellFormed(String input) {
		if(input == null || input.length() < PASSWORD_LOWER_CONSTRAIT ||
		   input.length() > PASSWORD_UPPER_CONSTRAIT) {
			return false;
		}
		return input.matches("([a-zA-Z0-9_\\s]*[\\W&&[^\\s+]][a-zA-Z0-9_\\s]*)+");
	}
	
	/**
	 * To validate email input
	 * Case insensitive.
	 * @param input
	 * 		email input
	 * @return whether the email is well formed
	 */
	public static boolean isEmailWellFormed(String input) {
		if(input == null || input.length() > EMAIL_UPPER_CONSTRAIT) {
			return false;
		}
		String uInput = input.toUpperCase();
		return uInput.matches("\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b");
	}
	
	/**
	 * To loosely validate Aces2 login 
	 */
	public static boolean isAces2LoginWellFormed(String username, String password) {
		return (username.length() < ACES2_UPPER_CONSTRAIT) && (password.length() < ACES2_UPPER_CONSTRAIT) &&
			   username.matches("[a-zA-Z]+[0-9]+");
	}
	
	/**
	 * To loosely validate general login 
	 */
	public static boolean isGeneralLoginWellFormed(String username, String password) {
		//something more
		return (username.length() < GENERAL_UPPER_CONSTRAIT) && (password.length() < GENERAL_UPPER_CONSTRAIT);
	}
	
}
